package com.system.user.web;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.user.entity.User;
import com.system.user.entity.DTO.UserQueryDTO;
import com.system.user.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController 
{
	@Autowired
	private IUserService userService;

	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(User user) 
	{	
//		Subject subject = SecurityUtils.getSubject();
//		if(!subject.isPermitted("user:saveOrUpdate")){
//			return new ExtAjaxResponse(false,"无权访问");
//		}
		if(user.getUid()==null) {
			try {
				userService.saveOrUpdate(user);
				return new ExtAjaxResponse(true,"用户添加成功");
			}catch(Exception e){
				return new ExtAjaxResponse(false,"用户添加失败");}
		}else{	
			try {
				userService.saveOrUpdate(user);
				return new ExtAjaxResponse(true,"用户信息更新成功");
			}catch (Exception e) {
				return new ExtAjaxResponse(false,"用户信息更新失败");
			}	}
	}
	@RequestMapping("/reset")
	public @ResponseBody ExtAjaxResponse reset(@RequestParam String id,@RequestParam String newPwd) 
	{
		try {
			User user = userService.findOne(id);
			user.setPassword(newPwd);
			userService.saveOrUpdate(user);
			return new ExtAjaxResponse(true,"密码更新成功");
		}catch (Exception e) {
			return new ExtAjaxResponse(true,"密码更新失败");
		}
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam String id) {
		try {
			User user = userService.findOne(id);
			userService.delete(user);
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}

		
	}
	@RequestMapping("/deleteUsers")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam String[] ids) {
		
		
		try {
			List<String> lists = Arrays.asList(ids);
			if (null!=lists) {
				userService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}

		
	}
	@RequestMapping("/findOne")
	public @ResponseBody User findOne(@RequestParam String id) 
	{
		User user =  userService.findOne(id);
		
		return user;
	}
	
	
	@RequestMapping("/findAll")
	public @ResponseBody List<User> findAll() 
	{
		 List<User> lists =  userService.findAll();
		
		return lists;
	}
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public @ResponseBody ExtAjaxResponse Login(HttpServletRequest request, @RequestParam String userName,@RequestParam String password) {
		HttpSession session = request.getSession();
		User user = userService.findUser(userName, password);
		if(null!=user) {
			session.setAttribute("userName",user.getUserName());
			session.setAttribute("userId",user.getUid());
			return new ExtAjaxResponse(true,"成功");
		}else {
			return new ExtAjaxResponse(false,"帐号或密码错误，登录失败");
		}	
	}
	@RequestMapping("/logout")
	public @ResponseBody ExtAjaxResponse logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("userName");
		return new ExtAjaxResponse(true,"成功");
	}
	@RequestMapping("/test")
	public @ResponseBody String test(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return (String) session.getAttribute("userId");
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<User> findPage(UserQueryDTO userQueryDTO,ExtPageable pageable) 
	{
		Page<User> page =  userService.findAll(UserQueryDTO.getSpecification(userQueryDTO), pageable.getPageable());
		
		return page;
	}
}
